package com.aorise.controller.camera;

import com.aorise.exception.DataValidationException;
import com.aorise.exception.ServiceException;
import com.aorise.model.camera.*;
import com.aorise.service.camera.CameraService;
import com.aorise.utils.ConstDefine;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.excel.ExcelUtil;
import com.aorise.utils.json.InnerResult;
import com.aorise.utils.json.JsonResponseData;
import com.aorise.utils.page.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.*;

/**
 * @Author:cat
 * @Desicription: 视频设备控制器类
 * @Date:Created in 2019-05-27
 * @Modified By:
 */
@RestController
@Api(description ="视频设备信息接口")
public class CameraController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CameraService cameraService;

    /**
     * 根据筛选条件进行分页查询视频设备信息
     * HTTP 方式：GET
     * API 路径：/api/camera/pageIndex/{pageIndex}/pageNum/{pageNum}
     * 方法名：GetAllCameraListByPage
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-05-27
     */
    @ApiOperation(value = "根据筛选条件进行分页查询视频设备信息", httpMethod = "GET", response = String.class, notes = "根据筛选条件进行分页查询视频设备信息,提供视频设备管理、一机一档-查询列表使用")
    @RequestMapping(value = "/api/camera/pageIndex/{pageIndex}/pageNum/{pageNum}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getAllCameraListByPage(@ApiParam(value = "设备名称", required = false) @RequestParam(value = "caName", required = false) String caName,
                                         @ApiParam(value = "设备编码", required = false) @RequestParam(value = "caNumber", required = false) String caNumber,
                                         @ApiParam(value = "起始IP地址", required = false) @RequestParam(value = "startIp", required = false) String startIp,
                                         @ApiParam(value = "截止IP地址", required = false) @RequestParam(value = "endIp", required = false) String endIp,
                                         @ApiParam(value = "MAC地址", required = false) @RequestParam(value = "mac", required = false) String mac,
                                         @ApiParam(value = "所属分组", required = false) @RequestParam(value = "groupId", required = false) Integer groupId,
                                         @ApiParam(value = "设备品牌：1海康，2大华，3宇视", required = false) @RequestParam(value = "brand", required = false) Integer brand,
                                         @ApiParam(value = "连接状态：1在线，2离线", required = false) @RequestParam(value = "online", required = false) Integer online,
                                         @ApiParam(value = "入网状态：1准入，2禁入", required = false) @RequestParam(value = "netIn", required = false) Integer netIn,
                                         @ApiParam(value = "页索引", required = true) @PathVariable(value = "pageIndex", required = true) String pageIndex,
                                         @ApiParam(value = "页大小", required = true) @PathVariable(value = "pageNum", required = true) String pageNum) {
        logger.debug("Request RESTful API:GetAllCameraListByPage");
        logger.debug("caName：" + caName);
        logger.debug("caNumber：" + caNumber);
        logger.debug("startIp：" + startIp);
        logger.debug("endIp：" + endIp);
        logger.debug("mac：" + mac);
        logger.debug("groupId：" + groupId);
        logger.debug("brand：" + brand);
        logger.debug("online：" + online);
        logger.debug("netIn：" + netIn);
        logger.debug("pageIndex" + pageIndex);
        logger.debug("pageNum" + pageNum);

        Page page = new Page();
        if (!StringUtils.isBlank(pageIndex)) {
            page.setCurrentPage(Long.parseLong(pageIndex.toString()));
        }
        if (!StringUtils.isBlank(pageNum)) {
            page.setEveryPage(Long.parseLong(pageNum.toString()));
        }
        try {

            Map<String, Object> map = new HashMap<>();
            map.put("caName", caName);
            map.put("caNumber", caNumber);
            map.put("startIp", startIp);
            map.put("endIp", endIp);
            map.put("mac", mac);
            map.put("groupId", groupId);
            map.put("brand", brand);
            map.put("online", online);
            map.put("netIn", netIn);
            map.put("halt", ConstDefine.HALT_NO);
            map.put("state", ConstDefine.STATE_ABLE);
            map.put("beginIndex", page.getBeginIndex());
            map.put("endinIndex", page.getEndinIndex());

            List<CameraModel> cameraModels = cameraService.getAllCameraListByPage(map, page);
            page.setList(cameraModels);

            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS, "", page).toString();
        } catch (DataAccessException e) {
            logger.error(
                    "controller:CameraController. function:GetAllCameraListByPage...msg:Get Camera List By Page DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), StatusDefine.DB_ERROR,
                    "", page).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:CameraController. function:GetAllCameraListByPage...msg:Get Camera List By Page Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), page).toString();
        }
    }

    /**
     * 根据ID查询视频设备详情
     * HTTP 方式：GET
     * API 路径：/api/camera/id/{id}
     * 方法名：getCameraById
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-05-27
     */
    @ApiOperation(value = "根据ID查询视频设备详情", httpMethod = "GET", response = String.class, notes = "根据ID查询视频设备详情，提供视频设备管理、一机一档-查询视频设备详情使用")
    @RequestMapping(value = "/api/camera/id/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCameraById(@ApiParam(value = "ID", required = true) @PathVariable(value = "id", required = true) Integer id,
                                @ApiParam(value = "是否受配置限制：1是，2否", required = true) @RequestParam(value = "flag", required = true) Integer flag) {

        logger.debug("Request RESTful API:getCameraById");
        logger.debug("id" + id);
        logger.debug("flag" + flag);
        try {
            CameraModel cameraModel = cameraService.getCameraById(id,flag);
            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS, "",cameraModel).toString();
        } catch (DataAccessException e) {
            logger.error(
                    "controller:CameraController. function:getCameraById...msg:Get Camera By Id DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), StatusDefine.DB_ERROR,
                    "", null).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:CameraController. function:getCameraById...msg:Get Camera By Id Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 新增视频设备信息
     * HTTP 方式：POST
     * API 路径：/api/camera
     * 方法名：addCamera
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-06-12
     */
    @ApiOperation(value = "新增视频设备信息", httpMethod = "POST", response = String.class, notes = "新增视频设备信息，提供一机一档-新增视频设备使用")
    @RequestMapping(value = "/api/camera", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    String addCamera(@RequestBody @Validated CameraAddVO cameraAddVO, BindingResult result) {

        logger.debug("Request RESTful API:addCamera");
        logger.debug("cameraAddVO：" + cameraAddVO);

        try {
            if (result.hasErrors()) {
                return JsonResponseData.ofMessage(StatusDefine.DATA_FORMAT_ERROR, result.getFieldError().getDefaultMessage());
            }
            /**替换空字符串值*/
            if (StringUtils.isBlank(cameraAddVO.getLongitude())) {
                cameraAddVO.setLongitude(null);
            }
            if (StringUtils.isBlank(cameraAddVO.getLatitude())) {
                cameraAddVO.setLatitude(null);
            }
            if (StringUtils.isBlank(cameraAddVO.getHeight())) {
                cameraAddVO.setHeight(null);
            }
            if (StringUtils.isBlank(cameraAddVO.getRadius())) {
                cameraAddVO.setRadius(null);
            }
            if (StringUtils.isBlank(cameraAddVO.getInstallationTime())) {
                cameraAddVO.setInstallationTime(null);
            }
            if (StringUtils.isBlank(cameraAddVO.getWarrantyPeriod())) {
                cameraAddVO.setWarrantyPeriod(null);
            }
            int ret = cameraService.addCamera(cameraAddVO);

            if (ret > 0) {
                return new JsonResponseData(true,
                        StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS, "","").toString();
            } else {
                return new JsonResponseData(false,
                        "", StatusDefine.FAILURE, StatusDefineMessage.GetMessage(StatusDefine.FAILURE),"").toString();
            }
        } catch (DataAccessException e) {
            logger.error(
                    "controller:CameraController. function:addCamera...msg:Add Camera DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), StatusDefine.DB_ERROR,
                    "", null).toString();
        } catch (ServiceException e) {
            logger.error(
                    "controller:CameraController. function:addCamera...msg:Add Camera ServiceException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false,"",
                    StatusDefine.CAMERA_NUMBER_EXIST, e.getMessage(), "").toString();
        } catch (Exception e) {
            logger.error(
                    "controller:CameraController. function:addCamera...msg:Add Camera Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 修改视频设备信息
     * HTTP 方式：PUT
     * API 路径：/api/camera
     * 方法名：updateCamera
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-05-27
     */
    @ApiOperation(value = "修改视频设备信息", httpMethod = "PUT", response = String.class, notes = "修改视频设备信息，提供一机一档-编辑视频设备使用")
    @RequestMapping(value = "/api/camera", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    String updateCamera(@RequestBody @Validated CameraEditVO cameraEditVO, BindingResult result) {

        logger.debug("Request RESTful API:updateCamera");
        logger.debug("cameraEditVO：" + cameraEditVO);

        try {
            if (result.hasErrors()) {
                return JsonResponseData.ofMessage(StatusDefine.DATA_FORMAT_ERROR, result.getFieldError().getDefaultMessage());
            }

            /**替换空字符串值*/
            if (StringUtils.isBlank(cameraEditVO.getLongitude())) {
                cameraEditVO.setLongitude(null);
            }
            if (StringUtils.isBlank(cameraEditVO.getLatitude())) {
                cameraEditVO.setLatitude(null);
            }
            if (StringUtils.isBlank(cameraEditVO.getHeight())) {
                cameraEditVO.setHeight(null);
            }
            if (StringUtils.isBlank(cameraEditVO.getRadius())) {
                cameraEditVO.setRadius(null);
            }
            if (StringUtils.isBlank(cameraEditVO.getInstallationTime())) {
                cameraEditVO.setInstallationTime(null);
            }
            if (StringUtils.isBlank(cameraEditVO.getWarrantyPeriod())) {
                cameraEditVO.setWarrantyPeriod(null);
            }

            int ret = cameraService.updateCamera(cameraEditVO);

            if (ret > 0) {
                return new JsonResponseData(true,
                        StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS, "","").toString();
            } else {
                return new JsonResponseData(false,
                        StatusDefineMessage.GetMessage(StatusDefine.FAILURE), StatusDefine.FAILURE, "","").toString();
            }
        } catch (ServiceException e) {
            logger.error(
                    "controller:CameraController. function:updateCamera...msg:Update Camera ServiceException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false,"",
                    StatusDefine.CAMERA_NUMBER_EXIST, e.getMessage(), "").toString();
        } catch (DataAccessException e) {
            logger.error(
                    "controller:CameraController. function:updateCamera...msg:Update Camera DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:CameraController. function:updateCamera...msg:Update Camera Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 导入视频设备信息数据
     * HTTP 方式：POST
     * API 路径：/api/company/importCamera
     * 方法名：importCamera
     * 方法返回类型：String
     * 方法异常：Exception
     *
     * @Author: cat
     * @Date: 2019-05-28
     */
    @ApiOperation(value = "导入视频设备信息数据", httpMethod = "POST", response = String.class, notes = "导入视频设备信息数据，提供一机一档-导入视频设备使用")
    @RequestMapping(value = "/api/camera/importCamera", method = RequestMethod.POST)
    public String importCamera(@ApiParam(value = "excel文件", required = true) @RequestParam(value = "excelFile", required = true) MultipartFile excelFile,
                               @ApiParam(value = "创建人", required = true) @RequestParam(value = "createUser", required = true) Integer createUser) {
        logger.debug("Request RESTful API:importCamera");
        logger.debug("createUser:" + createUser);

        try {
            InnerResult<String> result = cameraService.importCamera(excelFile, createUser);
            String msg = result.getObject().toString();
            if (result.getSuccess()) {
                return new JsonResponseData(true, msg, StatusDefine.SUCCESS, "", null).toString();
            } else {
                return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR, msg,
                        null).toString();
            }
        } catch (DataValidationException e) {
            logger.error(
                    "controller:CameraController. function:importCamera...msg:Import Camera DataValidationException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.FILE_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        } catch (DataAccessException e) {
            logger.error(
                    "controller:CameraController. function:importCamera...msg:Import Camera DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:CameraController. function:importCamera...msg:Import Camera Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 导出视频设备信息
     * HTTP 方式：GET
     * API 路径：/api/camera/exportCamera
     * 方法名：exportCamera
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-05-29
     */
    @ApiOperation(value = "导出视频设备信息", httpMethod = "GET", response = String.class, notes = "导出视频设备信息，提供一机一档-导出视频设备信息使用")
    @RequestMapping(value = "/api/camera/exportCamera", method = RequestMethod.GET)
    public String exportCamera(@ApiParam(value = "设备名称", required = false) @RequestParam(value = "caName", required = false) String caName,
                               @ApiParam(value = "设备编码", required = false) @RequestParam(value = "caNumber", required = false) String caNumber,
                               @ApiParam(value = "起始IP地址", required = false) @RequestParam(value = "startIp", required = false) String startIp,
                               @ApiParam(value = "截止IP地址", required = false) @RequestParam(value = "endIp", required = false) String endIp,
                               @ApiParam(value = "MAC地址", required = false) @RequestParam(value = "mac", required = false) String mac,
                               @ApiParam(value = "所属分组", required = false) @RequestParam(value = "groupId", required = false) Integer groupId,
                               @ApiParam(value = "设备品牌：1海康，2大华，3宇视", required = false) @RequestParam(value = "brand", required = false) Integer brand,
                               @ApiParam(value = "入网状态：1准入，2禁入", required = false) @RequestParam(value = "netIn", required = false) Integer netIn,
                               HttpServletResponse response) {
        logger.debug("Request RESTful API:exportCamera");
        logger.debug("caName：" + caName);
        logger.debug("caNumber：" + caNumber);
        logger.debug("startIp：" + startIp);
        logger.debug("endIp：" + endIp);
        logger.debug("mac：" + mac);
        logger.debug("groupId：" + groupId);
        logger.debug("brand：" + brand);
        logger.debug("netIn：" + netIn);
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("caName", caName);
            map.put("caNumber", caNumber);
            map.put("startIp", startIp);
            map.put("endIp", endIp);
            map.put("mac", mac);
            map.put("groupId", groupId);
            map.put("brand", brand);
            map.put("netIn", netIn);
            map.put("halt", ConstDefine.HALT_NO);
            map.put("state", ConstDefine.STATE_ABLE);

            cameraService.exportCamera(map, response);
            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS,
                    "", null).toString();
        } catch (DataAccessException e) {
            logger.error(
                    "controller:CameraController. function:exportCamera...msg:Export Camera DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:CameraController. function:exportCamera...msg:Export Camera Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 下载导入视频设备信息模板
     * HTTP 方式：GET
     * API 路径：/api/camera/download
     * 方法名：exportCamera
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-05-29
     */
    @ApiOperation(value = "下载导入视频设备信息模板", httpMethod = "GET", response = String.class, notes = "下载导入视频设备信息模板，提供一机一档-下载模板使用")
    @RequestMapping(value = "/api/camera/download", method = RequestMethod.GET)
    public String downloadCameraTemplate(HttpServletResponse response) {
        logger.debug("Request RESTful API:downloadCameraTemplate");

        try {
            cameraService.downloadCameraTemplate(response);
            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS,
                    "", null).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:CameraController. function:downloadCameraTemplate...msg:Download Camera Template Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        }
    }

    /**
     * 批量修改视频设备的入网状态
     * HTTP 方式：PUT
     * API 路径：/api/camera/updateCameraStatus
     * 方法名：updateCameraStatus
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-05-30
     */
    @ApiOperation(value = "批量修改视频设备的入网状态", httpMethod = "PUT", response = String.class, notes = "批量修改视频设备的入网状态，提供视频设备管理-准入网络、禁入网络使用")
    @RequestMapping(value = "/api/camera/updateCameraStatus", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    String updateCameraStatus(@ApiParam(value = "设备主键ID（逗号分隔）", required = true) @RequestParam(value = "id", required = true) String id,
                              @ApiParam(value = "入网状态：1准入，2禁入", required = true) @RequestParam(value = "netIn", required = true) Integer netIn,
                              @ApiParam(value = "修改人", required = true) @RequestParam(value = "editUser", required = true) Integer editUser) {

        logger.debug("Request RESTful API:updateCameraStatus");
        logger.debug("id：" + id);
        logger.debug("netIn：" + netIn);
        logger.debug("editUser：" + editUser);

        try {
            Map<String, Object> map = new HashMap<>();
            map.put("id", id);
            map.put("netIn", netIn);
            map.put("editUser", editUser);
            map.put("state", ConstDefine.STATE_ABLE);
            int ret = cameraService.updateCameraStatus(map);

            if (ret > 0) {
                return new JsonResponseData(true,
                        StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS, "","").toString();
            } else {
                return new JsonResponseData(false,
                        "", StatusDefine.FAILURE, StatusDefineMessage.GetMessage(StatusDefine.FAILURE),"").toString();
            }
        } catch (DataAccessException e) {
            logger.error(
                    "controller:CameraController. function:updateCameraStatus...msg:Update Camera Status DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:CameraController. function:updateCameraStatus...msg:Update Camera Status Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 根据筛选条件查询视频设备信息
     * HTTP 方式：GET
     * API 路径：/api/camera
     * 方法名：getCameraList
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-06-01
     */
    @ApiOperation(value = "根据筛选条件查询视频设备信息", httpMethod = "GET", response = String.class, notes = "根据筛选条件查询视频设备信息,提供点位迁建、计划停机指定设备使用")
    @RequestMapping(value = "/api/camera", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCameraList(@ApiParam(value = "设备名称", required = false) @RequestParam(value = "caName", required = false) String caName,
                                @ApiParam(value = "设备编码", required = false) @RequestParam(value = "caNumber", required = false) String caNumber,
                                @ApiParam(value = "所属分组", required = false) @RequestParam(value = "groupId", required = false) Integer groupId,
                                @ApiParam(value = "维修次数", required = false) @RequestParam(value = "serviceCount", required = false) Integer serviceCount) {
        logger.debug("Request RESTful API:GetAllCameraListByPage");
        logger.debug("caName：" + caName);
        logger.debug("caNumber：" + caNumber);
        logger.debug("groupId：" + groupId);
        logger.debug("serviceCount：" + serviceCount);

        try {

            Map<String, Object> map = new HashMap<>();
            map.put("caName", caName);
            map.put("caNumber", caNumber);
            map.put("groupId", groupId);
            map.put("serviceCount", serviceCount);
            map.put("halt", ConstDefine.HALT_NO);
            map.put("state", ConstDefine.STATE_ABLE);

            List<CameraModel> cameraModels = cameraService.getCameraList(map);

            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS, "", cameraModels).toString();
        } catch (DataAccessException e) {
            logger.error(
                    "controller:CameraController. function:GetAllCameraListByPage...msg:Get Camera List By Page DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:CameraController. function:GetAllCameraListByPage...msg:Get Camera List By Page Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 每天0点1分更新视频设备坐标、停机状态
     * @Author: cat
     * @Date: 2019-06-12
     */
    @Scheduled(cron = "0 */5 * * * ?")
    //@Scheduled(cron = "0 1 0 * * ?")
    private void updateCameraAttribute(){
        try {
            logger.info("更新视频设备坐标、停机状态开始,当前时间为: {}",new Date());
            cameraService.updateCameraAttribute();
            logger.info("更新视频设备坐标、停机状态结束,当前时间为: {}",new Date());
        }catch (DataAccessException e) {
            logger.error(
                    "controller:CameraController. function:updateCameraAttribute...msg: Update Camera Attribute DataAccessException");
            logger.error("error:" + e.getMessage());
        } catch (Exception e) {
            logger.error(
                    "controller:CameraController. function:updateCameraAttribute... msg:Update Camera Attribute Exception");
            logger.error(e.getMessage());
        }
    }

}
